package com.example.common;


import lombok.Data;

import java.io.Serial;
import java.io.Serializable;

/**
 * Created with IntelliJ IDEA 2023.
 * Description:
 * User: homechen
 * Date: 2024-04-12
 * Time: 14:17
 */
@Data
public class AjaxResult implements Serializable {
    @Serial
    private static final long serialVersionUID = -3166356399586188236L;  //序列化版本号

    private int code; // 状态码
    private String msg; // 状态码描述
    private Object data; // 返回结果

    /**
     * 成功返回的对象
     */
    public static AjaxResult success(String msg, Object data) {
        AjaxResult result = new AjaxResult();
        result.setCode(200);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    public static AjaxResult success(Object data) {
        AjaxResult result = new AjaxResult();
        result.setCode(200);
        result.setMsg("");
        result.setData(data);
        return result;
    }

    /**
     * 失败返回的结果
     */
    public static AjaxResult fail(int code, String msg, Object data) {
        AjaxResult result = new AjaxResult();
        result.setCode(code);
        result.setData(data);
        result.setMsg(msg);
        return result;
    }

    public static AjaxResult fail(int code, String msg) {
        AjaxResult result = new AjaxResult();
        result.setCode(code);
        result.setData(null);
        result.setMsg(msg);
        return result;
    }
}
